
from pages.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select


class ContactsPage(BasePage):
    url = "/crm/index.php?m=contacts&a=index"

    add_contact_locator = (By.XPATH, '/html/body/div[5]/div[2]/div[1]/div/a')  # 新建联系人
    result_locator = (By.XPATH, '/html/body/div[5]/div[2]')
    error_locator =(By.XPATH,"/html/body/div/form/fieldset/div[1]")
    # 搜索
    field_locator = (By.ID, 'field')  # 选择筛选条件
    condition_locator = (By.ID, 'condition')  # 默认值为包含的选择下拉框
    search_locator = (By.ID, 'search')  # 搜索输入框
    do_search_locator = (By.CSS_SELECTOR, '#searchForm > ul > li:nth-child(4) > button > img')  # 搜索按钮
    have_inform_locator = (By.CSS_SELECTOR, '#form1 > table')  # 存在数据
    # 以下是视图部分的定位器
    all_locator = (By.LINK_TEXT, '全部')  # 全部
    contact_today_locator = (By.LINK_TEXT, '今日添加')  # 今日添加
    contact_week_locator = (By.LINK_TEXT, '本周添加')  # 本周添加
    contact_month_locator = (By.LINK_TEXT, '本月添加')  # 本月添加
    recently_created_locator = (By.LINK_TEXT, '最近创建')  # 最近创建
    recently_update_locator = (By.LINK_TEXT, '最近更新')  # 最近更新
    def click_add_contact(self):
        """
        点击新建联系人
        """
        self.find_element(self.add_contact_locator).click()
    def select_field(self, field):
        """
        选择筛选条件
        """
        field_element = Select(self.find_element(self.field_locator))
        field_element.select_by_visible_text(field)

    def select_condition(self, condition):
        """
        选择条件
        """
        condition_element = Select(self.find_element(self.condition_locator))
        condition_element.select_by_visible_text(condition)

    def input_search_key(self, clue_search_key):
        """
        输入搜索词
        """
        self.find_element(self.search_locator).send_keys(clue_search_key)

    def click_do_search(self):
        """
        点击搜索按钮，搜索线索
        """
        self.find_element(self.do_search_locator).click()

    def have_inform_text(self):
        """
        搜索后存在数据
        """
        return self.find_element(self.have_inform_locator).text

    def click_all(self):
        """
        点击视图——全部
        """
        self.find_element(self.all_locator).click()

    def click_contact_today(self):
        """
        点击视图——今日添加
        """
        self.find_element(self.contact_today_locator).click()

    def click_contact_week(self):
        """
        点击视图——本周添加
        """
        self.find_element(self.contact_week_locator).click()

    def click_contact_month(self):
        """
        点击视图——本月添加
        """
        self.find_element(self.contact_month_locator).click()

    def click_recently_create(self):
        """
        点击视图——最近创建
        """
        self.find_element(self.recently_created_locator).click()

    def click_recently_update(self):
        """
        点击视图——最近更新
        """
        self.find_element(self.recently_update_locator).click()